﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;
using TradeForm.Properties;
using System.Configuration;
using TradeForm.config;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using System.Globalization;
namespace TradeForm.Print
{
    public class PackListDocument : DocumentBase
    {
        //放置配置的字典
        protected Dictionary<string, Setting> dicPrintSets = new Dictionary<string, Setting>();

        public PackListDocument()
        { }
        public PackListDocument(string defcode)
        {
            this._defCode = defcode;
            getData(defcode);
            loadPrintSettings();

        }

        public PackListDocument(string defcode, bool isPrintBack)
        {
            this.IsPrintBack = isPrintBack;
            this._defCode = defcode;
            getData(defcode);
            loadPrintSettings();
        }

        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            g.PageUnit = GraphicsUnit.Millimeter;
            float fontHeight = g.MeasureString("字体测试高度", base.Font).Height;
            if (this.IsPrintBack)
            {
                g.DrawImage(Image.FromFile(Application.StartupPath + @"\BackImage\装箱单.jpg"), 0, 0, Convert.ToSingle(e.PageBounds.Width * 25.4 / 100),
                   Convert.ToSingle(e.PageBounds.Height * 25.4 / 100));
            }
            if (this.pack != null && this.packGoodsList != null && this.packGoodsList.Count > 0)
            {
                g.DrawString(pack.Mark, base.Font, Brushes.Black, dicPrintSets["Mark"].PosX, dicPrintSets["Mark"].PosY);
                //g.DrawString(pack.TotalGoodsName, base.Font, Brushes.Black, dicPrintSets["GoodsCName"].PosX, dicPrintSets["GoodsCName"].PosY);
                g.DrawString(pack.InvoiceNo, base.Font, Brushes.Black, dicPrintSets["InvoiceNo"].PosX, dicPrintSets["InvoiceNo"].PosY);
                g.DrawString(pack.ContractNo, base.Font, Brushes.Black, dicPrintSets["ContractNo"].PosX, dicPrintSets["ContractNo"].PosY);
                CultureInfo ci = new CultureInfo("en-US");
                g.DrawString(pack.PackDate.Value.ToString("MMM dd,yyyy", ci), base.Font, Brushes.Black, dicPrintSets["Date"].PosX, dicPrintSets["Date"].PosY);
                //g.DrawString(pack.TotalCount.Value.ToString("F2"), base.Font, Brushes.Black, dicPrintSets["TotalCount"].PosX, dicPrintSets["TotalCount"].PosY);
                int i = 0;
                float goodspan = 5.23f;
                decimal totalQty = 0;
                decimal totalGW = 0.0m;
                //decimal totalMeas = 0.0m;
                decimal totalNW = 0.0m;
                decimal totalCtns = 0.0m;
                string qtyUnit = string.Empty;
                float strLength = 0.0f;
                foreach (PackListGoods item in this.packGoodsList)
                {
                    g.DrawString(item.CaseNo, base.Font, Brushes.Black, dicPrintSets["CaseNo"].PosX, dicPrintSets["CaseNo"].PosY + i * goodspan);

                    strLength = g.MeasureString(item.Ctns.Value.ToString("##0"), base.Font).Width;
                    g.DrawString(item.Ctns.Value.ToString("##0"), base.Font, Brushes.Black, dicPrintSets["Ctns"].PosX - strLength,
                        dicPrintSets["Ctns"].PosY + i * goodspan);
                    g.DrawString(item.PONo, base.Font, Brushes.Black, dicPrintSets["PONo"].PosX,
                        dicPrintSets["PONo"].PosY + i * goodspan);
                    g.DrawString(item.PkgDesc, base.Font, Brushes.Black, dicPrintSets["PkgDesc"].PosX,
                        dicPrintSets["PkgDesc"].PosY + i * goodspan);

                    strLength = g.MeasureString(item.Qty.Value.ToString("##0") + item.QtyUnit, base.Font).Width;
                    g.DrawString(item.Qty.Value.ToString("##0") + item.QtyUnit, base.Font, Brushes.Black, dicPrintSets["Qty"].PosX - strLength,
                        dicPrintSets["Qty"].PosY + i * goodspan);
                    if (string.IsNullOrEmpty(qtyUnit))
                    {
                        qtyUnit = item.QtyUnit;
                    }

                    strLength = g.MeasureString(item.GrossWeight.Value.ToString("##0.00"), base.Font).Width;
                    g.DrawString(item.GrossWeight.Value.ToString("##0.00"), base.Font, Brushes.Black, dicPrintSets["GrossWeight"].PosX - strLength,
                        dicPrintSets["GrossWeight"].PosY + i * goodspan);

                    strLength = g.MeasureString(item.NetWeight.Value.ToString("##0.00"), base.Font).Width;
                    g.DrawString(item.NetWeight.Value.ToString("##0.00"), base.Font, Brushes.Black, dicPrintSets["NetWeight"].PosX - strLength,
                        dicPrintSets["NetWeight"].PosY + i * goodspan);
                    totalGW += (item.GrossWeight.HasValue ? item.GrossWeight.Value : 0.0m);
                    //totalMeas += (string.IsNullOrEmpty(item.Meas) ? 0.0m : Convert.ToDecimal(item.Meas));
                    totalNW += (item.NetWeight.HasValue ? item.NetWeight.Value : 0.0m);
                    totalQty += (item.Qty.HasValue ? item.Qty.Value : 0.0m);
                    totalCtns += (item.Ctns.HasValue ? item.Ctns.Value : 0.0m);
                    strLength = 0.0f;
                    i++;
                }
                strLength = g.MeasureString(totalCtns.ToString("0") + "CTNS", base.Font).Width;
                g.DrawString(totalCtns.ToString("0") + "CTNS", base.Font, Brushes.Black, dicPrintSets["Ctns"].PosX - strLength
                            , dicPrintSets["Totals"].PosY);

                strLength = g.MeasureString(totalQty.ToString("0") + qtyUnit, base.Font).Width;
                g.DrawString(totalQty.ToString("0") + qtyUnit, base.Font, Brushes.Black, dicPrintSets["Qty"].PosX - strLength, dicPrintSets["Totals"].PosY);

                strLength = g.MeasureString(totalGW.ToString("F2"), base.Font).Width;
                g.DrawString(totalGW.ToString("F2"), base.Font, Brushes.Black, dicPrintSets["GrossWeight"].PosX - strLength, dicPrintSets["Totals"].PosY);

                strLength = g.MeasureString(totalNW.ToString("F2"), base.Font).Width;
                g.DrawString(totalNW.ToString("F2"), base.Font, Brushes.Black, dicPrintSets["NetWeight"].PosX - strLength, dicPrintSets["Totals"].PosY);
                // g.DrawString(pack.Remark, base.Font, Brushes.Black, dicPrintSets["PackRemark"].PosX, dicPrintSets["PackRemark"].PosY);
                g.DrawString("TOTAL QUANTITY:" + totalQty.ToString("0") + qtyUnit, base.Font, Brushes.Black, dicPrintSets["PackRemark"].PosX, dicPrintSets["PackRemark"].PosY + goodspan);
                g.DrawString("PACKED IN " + totalCtns.ToString("0") + " CARTONS ONLY", base.Font, Brushes.Black, dicPrintSets["PackRemark"].PosX, dicPrintSets["PackRemark"].PosY + 2 * goodspan);
                g.DrawString("GROSS WEIGHT:" + totalGW.ToString("F2") + " KGS", base.Font, Brushes.Black, dicPrintSets["PackRemark"].PosX, dicPrintSets["PackRemark"].PosY + 3 * goodspan);
                g.DrawString("NET WEIGHT:" + totalNW.ToString("F2") + " KGS", base.Font, Brushes.Black, dicPrintSets["PackRemark"].PosX, dicPrintSets["PackRemark"].PosY + 4 * goodspan);
                g.DrawString("MEASUREMENT:" + (goodsSum.TotalMeas.HasValue ? goodsSum.TotalMeas.Value.ToString("F4") : "0.0") + " M3", base.Font, Brushes.Black, dicPrintSets["PackRemark"].PosX, dicPrintSets["PackRemark"].PosY + 5 * goodspan);
            }
            if (this._compny != null)
            {
                float widthCName = g.MeasureString(_compny.CName, base.TitleFont).Width;
                float widthEName = g.MeasureString(_compny.EName, base.TitleFont).Width;
                float widthEAddress = g.MeasureString(_compny.EAddress, base.Font).Width;
                float widthTel = g.MeasureString(_compny.Tel, base.Font).Width;

                g.DrawString(_compny.CName, base.TitleFont, Brushes.Black, Convert.ToSingle((210.0 - widthCName) / 2), dicPrintSets["CompanyCName"].PosY);
                g.DrawString(_compny.EName, base.TitleFont, Brushes.Black, Convert.ToSingle((210.0 - widthEName) / 2), dicPrintSets["CompanyEName"].PosY);
                g.DrawString(_compny.EAddress, base.Font, Brushes.Black, Convert.ToSingle((210.0 - widthEAddress) / 2), dicPrintSets["EAddress"].PosY);
                g.DrawString(_compny.Tel, base.Font, Brushes.Black, Convert.ToSingle((210.0 - widthTel) / 2), dicPrintSets["Tel"].PosY);
                g.DrawString(_compny.CName, base.Font, Brushes.Black, dicPrintSets["CompanyCName2"].PosX, dicPrintSets["CompanyCName2"].PosY);
                g.DrawString(_compny.EName, base.Font, Brushes.Black, dicPrintSets["CompanyEName2"].PosX, dicPrintSets["CompanyEName2"].PosY);

            }
        }

        protected void loadPrintSettings()
        {
            XElement printSettings = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + @"\config\PackListSettings.xml");
            IEnumerable<XElement> settings = null;
            settings = (from el in printSettings.Elements("setting")
                        select el);
            if (settings != null && settings.Count() > 0)
            {
                foreach (XElement item in settings)
                {
                    if (!dicPrintSets.ContainsKey(item.Attribute("name").Value))
                    {

                        Setting set = new Setting();
                        set.Name = item.Attribute("name").Value;
                        set.CName = item.Attribute("cname").Value;
                        set.PosX = Convert.ToSingle(item.Attribute("PosX").Value);
                        set.PosY = Convert.ToSingle(item.Attribute("PosY").Value);
                        dicPrintSets.Add(set.Name, set);
                    }
                    else
                    {
                        dicPrintSets[item.Attribute("name").Value].CName = item.Attribute("cname").Value;
                        dicPrintSets[item.Attribute("name").Value].PosX = Convert.ToSingle(item.Attribute("PosX").Value);
                        dicPrintSets[item.Attribute("name").Value].PosY = Convert.ToSingle(item.Attribute("PosY").Value);
                    }
                }

            }
            else
            {

            }

        }
    }


}
